#ifndef LOG_ENABLED_HPP
#define LOG_ENABLED_HPP

/**
 * \file
 * \ingroup logging
 * SLOG and related logging macro definitions.
 */

#include "utils/mock-printer.hpp"
#include "core/log.hpp"

// These two implementation macros
//   SLOG_APPEND_TIME_PREFIX_IMPL
//   SLOG_APPEND_NODE_PREFIX_IMPL
// need to be defined in all configurations (debug, release, optimized)
// for use by NS_FATAL_...

/**
 * \ingroup logging
 * Implementation details for SLOG_APPEND_TIME_PREFIX.
 * \internal
 * Logging implementation macro; should not be called directly.
 * We define this separately so we can reuse the definition
 * in NS_FATAL.
 */
#define SLOG_APPEND_TIME_PREFIX_IMPL                           \
    do                                                         \
    {                                                          \
        const cas::TimePrinter ptr = cas::LogGetTimePrinter(); \
        if (ptr != 0)                                          \
        {                                                      \
            (*ptr)(std::clog);                                 \
            std::clog << " ";                                  \
        }                                                      \
    } while (false)

/**
 * \ingroup logging
 * Implementation details for SLOG_APPEND_NODE_PREFIX.
 * \internal
 * Logging implementation macro; should not be called directly.
 * We define this separately so we can reuse the definition
 * in NS_FATAL.
 */
#define SLOG_APPEND_NODE_PREFIX_IMPL                           \
    do                                                         \
    {                                                          \
        const cas::NodePrinter ptr = cas::LogGetNodePrinter(); \
        if (ptr != 0)                                          \
        {                                                      \
            (*ptr)(std::clog);                                 \
            std::clog << " ";                                  \
        }                                                      \
    } while (false)

#ifdef SLOG_ENABLE

/**
 * \ingroup logging
 * Append the simulation time to a log message.
 * \internal
 * Logging implementation macro; should not be called directly.
 */
#define SLOG_APPEND_TIME_PREFIX                \
    if (g_log.IsEnabled(cas::LOG_PREFIX_TIME)) \
    {                                          \
        SLOG_APPEND_TIME_PREFIX_IMPL;          \
    }

/**
 * \ingroup logging
 * Append the simulation node id to a log message.
 * \internal
 * Logging implementation macro; should not be called directly.
 */
#define SLOG_APPEND_NODE_PREFIX                \
    if (g_log.IsEnabled(cas::LOG_PREFIX_NODE)) \
    {                                          \
        SLOG_APPEND_NODE_PREFIX_IMPL;          \
    }

/**
 * \ingroup logging
 * Append the function name to a log message.
 * \internal
 * Logging implementation macro; should not be called directly.
 */
#define SLOG_APPEND_FUNC_PREFIX                \
    if (g_log.IsEnabled(cas::LOG_PREFIX_FUNC)) \
    {                                          \
        std::clog << g_log.Name() << ":"       \
                  << __FUNCTION__ << "(): ";   \
    }

/**
 * \ingroup logging
 * Append the log severity level to a log message.
 * \internal
 * Logging implementation macro; should not be called directly.
 */
#define SLOG_APPEND_LEVEL_PREFIX(level)                         \
    if (g_log.IsEnabled(cas::LOG_PREFIX_LEVEL))                 \
    {                                                           \
        std::clog << "[" << g_log.GetLevelLabel(level) << "] "; \
    }

#ifndef SLOG_APPEND_CONTEXT
/**
 * \ingroup logging
 * Append the node id (or other file-local programmatic context, such as
 * MPI rank) to a log message.
 *
 * This is implemented locally in `.cc` files because
 * the relevant variable is only known there.
 *
 * Preferred format is something like (assuming the node id is
 * accessible from `var`:
 * \code
 *   if (var)
 *     {
 *       std::clog << "[node " << var->GetObject<Node> ()->GetId () << "] ";
 *     }
 * \endcode
 */
#define SLOG_APPEND_CONTEXT
#endif /* SLOG_APPEND_CONTEXT */

#ifndef SLOG_CONDITION
/**
 * \ingroup logging
 * Limit logging output based on some file-local condition,
 * such as MPI rank.
 *
 * This is implemented locally in `.cc` files because
 * the relevant condition variable is only known there.
 *
 * Since this appears immediately before the `do { ... } while false`
 * construct of \c SLOG(level, msg), it must have the form
 * \code
 *   #define SLOG_CONDITION  if (condition)
 * \endcode
 */
#define SLOG_CONDITION
#endif

/**
 * \ingroup logging
 *
 * This macro allows you to log an arbitrary message at a specific
 * log level.
 *
 * The log message is expected to be a C++ ostream
 * message such as "my string" << aNumber << "my oth stream".
 *
 * Typical usage looks like:
 * \code
 * SLOG(LOG_DEBUG, "a number="<<aNumber<<", anotherNumber="<<anotherNumber);
 * \endcode
 *
 * \param [in] level The log level
 * \param [in] msg The message to log
 * \internal
 * Logging implementation macro; should not be called directly.
 */
#define SLOG(level, msg)                     \
    SLOG_CONDITION                           \
    do                                       \
    {                                        \
        if (g_log.IsEnabled(level))          \
        {                                    \
            SLOG_APPEND_TIME_PREFIX;         \
            SLOG_APPEND_NODE_PREFIX;         \
            SLOG_APPEND_CONTEXT;             \
            SLOG_APPEND_FUNC_PREFIX;         \
            SLOG_APPEND_LEVEL_PREFIX(level); \
            std::clog << msg << std::endl;   \
        }                                    \
    } while (false)

/**
 * \ingroup logging
 *
 * Output the name of the function.
 *
 * This should be used only in static functions; most member functions
 * should instead use NS_LOG_FUNCTION().
 */
#define SLOG_FUNCTION_NOARGS()                                                     \
    SLOG_CONDITION                                                                 \
    do                                                                             \
    {                                                                              \
        if (g_log.IsEnabled(cas::LOG_FUNCTION))                                    \
        {                                                                          \
            SLOG_APPEND_TIME_PREFIX;                                               \
            SLOG_APPEND_NODE_PREFIX;                                               \
            SLOG_APPEND_CONTEXT;                                                   \
            std::clog << g_log.Name() << ":" << __FUNCTION__ << "()" << std::endl; \
        }                                                                          \
    } while (false)

/**
 * \ingroup logging
 *
 * If log level LOG_FUNCTION is enabled, this macro will output
 * all input parameters separated by ", ".
 *
 * Typical usage looks like:
 * \code
 * SLOG_FUNCTION(num1 << num2);
 * \endcode
 * And the output will look like:
 * \code
 * Component:Function(num1, num2)
 * \endcode
 *
 * To facilitate function tracing, most functions should begin with (at least)
 * SLOG_FUNCTION(this). Static functions should use SLOG_FUNCTION_NOARGS() instead.
 *
 * \param [in] parameters The parameters to output.
 */
#define SLOG_FUNCTION(parameters)                                    \
    SLOG_CONDITION                                                   \
    do                                                               \
    {                                                                \
        if (g_log.IsEnabled(cas::LOG_FUNCTION))                      \
        {                                                            \
            SLOG_APPEND_TIME_PREFIX;                                 \
            SLOG_APPEND_NODE_PREFIX;                                 \
            SLOG_APPEND_CONTEXT;                                     \
            std::clog << g_log.Name() << ":" << __FUNCTION__ << "("; \
            cas::ParameterLogger(std::clog) << parameters;           \
            std::clog << ")" << std::endl;                           \
        }                                                            \
    } while (false)

/**
 * \ingroup logging
 *
 * Output the requested message unconditionally.
 *
 * \param [in] msg The message to log
 */
#define SLOG_UNCOND(msg)               \
    SLOG_CONDITION                     \
    do                                 \
    {                                  \
        std::clog << msg << std::endl; \
    } while (false)

#endif /* NS3_LOG_ENABLE */

#endif /* LOG_ENABLED_HPP */
